package com.example.shushiyunsst.fragment.main.transaction.child

import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.annotation.RequiresApi
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.alibaba.fastjson.JSON
import com.example.library_common.album.utils.Utils
import com.example.library_common.base.LazyFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.global.AppGlobals
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.transaction.bean.TransactionUIInvokeListener
import com.example.shushiyunsst.fragment.main.transaction.child.content.TransactionSettingFragment
import com.example.shushiyunsst.fragment.main.transaction.child.menu.MenuLabInfo
import com.example.shushiyunsst.fragment.main.transaction.child.menu.SelectedMoneyTypeFragment
import com.example.shushiyunsst.fragment.main.wallet.bean.MyCaptureBean
import com.example.shushiyunsst.fragment.main.wallet.sub.WalletLabInfo
import com.example.shushiyunsst.widget.verification.NoScrollViewPager
import com.google.android.material.tabs.TabLayout
import ikidou.reflect.TypeBuilder

class TransactionMenuFragment : LazyFragment() {

    companion object {
        fun newInstance(): TransactionMenuFragment {
            val args = Bundle()
            val fragment = TransactionMenuFragment()
            fragment.arguments = args
            return fragment
        }
    }

    private var transactionUIInvokeListener: TransactionUIInvokeListener? = null

    fun setTransactionUIInvokeListener(listener: TransactionUIInvokeListener?): TransactionMenuFragment {
        this.transactionUIInvokeListener = listener
        return this
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mSuperBackPressedSupport=false
    }

    private lateinit var mNoScrollViewPager: NoScrollViewPager

    private lateinit var mTabLayout: TabLayout

    override fun getLayoutId() = R.layout.transaction_menu_fragment_layout

    @RequiresApi(Build.VERSION_CODES.KITKAT)
    override fun lazyInit() {

        val jsonFromRaw = Utils.getJsonFromRaw(_mActivity, R.raw.transaction_menu_selected)

        val build = TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(String::class.java).endSubType().build()

        val bean: CommonBean<List<String>> = JSON.parseObject(jsonFromRaw, build)

        val data = bean.data

        val fragments = mutableListOf<Fragment>()

        data.forEach {

            fragments.add(
                SelectedMoneyTypeFragment.newInstance(it)
                    .setTransactionUIInvokeListener(transactionUIInvokeListener)
            )
        }

        mNoScrollViewPager.adapter = object
            :
            FragmentStatePagerAdapter(childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

            override fun getCount() = fragments.size

            override fun getItem(position: Int) = fragments[position]

            override fun getPageTitle(position: Int): CharSequence? {
                val walletLabInfo = getItem(position) as MenuLabInfo
                return walletLabInfo.title
            }

        }

        mTabLayout.setupWithViewPager(mNoScrollViewPager)
    }

    override fun initView(view: View) {
        mNoScrollViewPager = view.findViewById(R.id.mNoScrollViewPager)
        mTabLayout = view.findViewById(R.id.mTabLayout)
        mNoScrollViewPager.setNoScroll(true)
    }

    override fun onDestroy() {
        super.onDestroy()
        transactionUIInvokeListener = null
    }
}